﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.OrderPlan.Models;
using Things.OrderPlan;
using System.Text;

namespace Things.Web.OrderPlan
{
    public partial class SelectSet : System.Web.UI.Page
    {
        private Things.Model.GoodsPrice _goodsPrice;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid goodsId = new Guid(Request.QueryString["goodsid"]);

                Things.BLL.GoodsPrice bll = new Things.BLL.GoodsPrice();
                StringBuilder strWhere = new StringBuilder();
                strWhere.Append(" GoodsID='" + goodsId.ToString() + "'");
                List<Things.Model.GoodsPrice> models = bll.GetModelList(strWhere.ToString());
                if (models != null && models.Count > 0)
                {
                    _goodsPrice = models[0];
                }

                List<SaleSet> sets = new SaleSetRepository().FindAll(goodsId);

                DataList1.DataSource = sets;
                DataList1.DataBind();

                BindData(goodsId);
            }
        }

        protected void BindData(Guid GoodsID)
        {
            GoodsRelationInforOP1.GoodsID = GoodsID;
        }

        public string GetA()
        {
            Guid goodsId = new Guid(Request.QueryString["goodsid"]);
            Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
            if (parameter != null)
            {
                return Math.Round((parameter.P1 - ((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1))), 2).ToString();
            }
            return "1";
        }

        public string GetB()
        {
            Guid goodsId = new Guid(Request.QueryString["goodsid"]);
            Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
            if (parameter != null)
            {
                return Math.Round(((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1)), 2).ToString();
            }
            return "0";
        }

        public string GetCB()
        {
            return _goodsPrice.GoodsCurrentPrice.ToString();
        }

        public string GetPN()
        {
            Guid goodsId = new Guid(Request.QueryString["goodsid"]);
            Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
            if (parameter != null)
            {
                return Math.Round(parameter.PN, 2).ToString();
            }
            return "0";
        }

        public string GetTotalPrice(object unitPrice, object deliveredNumber, object deliveredCount, object deliveredCycle)
        {
            Double up = Convert.ToDouble(unitPrice);
            int dNumber = Convert.ToInt32(deliveredNumber);
            int dCount = Convert.ToInt32(deliveredCount);
            //int dCycle = Convert.ToInt32(deliveredCycle);

            //return Math.Round(up * dNumber * dCount * dCycle, 2).ToString();           

            return Math.Round(up * dNumber * dCount, 2).ToString();
        }

        public string GetTotalCount(object deliveredNumber, object deliveredCount, object deliveredCycle)
        {
            int dNumber = Convert.ToInt32(deliveredNumber);
            int dCount = Convert.ToInt32(deliveredCount);
            //int dCycle = Convert.ToInt32(deliveredCycle);

            //return (dNumber * dCount * dCycle).ToString();
            
            return (dNumber * dCount ).ToString();
        }

        public string GetDiscount(object unitPrice, object goodsId)
        {
            double price = Convert.ToDouble(unitPrice);
            Guid id = new Guid(goodsId.ToString());

            if (_goodsPrice != null)
            {
                double discount = price / (double)_goodsPrice.GoodsCurrentPrice;
                return Math.Round(discount * 10, 1).ToString();
            }
            return "--";
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Guid goodsId = new Guid(Request.QueryString["goodsid"]);
                int number = Convert.ToInt32(_ctlEditNumber.Text);
                int count = Convert.ToInt32(_ctlEditCycle.Text);
                int cycle = Convert.ToInt32(_ctlEditCount.Text);
                
                //
                OrderPlanCartUtil.RemoveAll(this.Context);

                //
                OrderPlanCartUtil.Ordering(
                    this.Context,
                    Guid.Empty,
                    goodsId,
                    count,
                    cycle,
                    number,
                    "~/OrderPlan/OrderPlanCart.aspx");
            }
            catch
            {
            }
        }
    }
}